<script setup lang="ts" name="Home">
  // 导入watch
  import {ref,watch} from 'vue'
  let sum = ref(0)

  //1. watch监听ref定义的基本数据类型=>value值的改变
  // watch(sum,(newValue,oldValue)=>{
  //   console.log(`sum变化了,改变前:${oldValue},改变后:${newValue}`)
  // })

  //补充: 终止监听
  const stopWatch = watch(sum,(newValue,oldValue)=>{
    console.log(`sum变化了,改变前:${oldValue},改变后:${newValue}`)
    if(newValue>=5){
      stopWatch()//终止监听
    }
  })
  
</script>


<template>
    <button @click="sum++">sum+1</button>
    <h1>初始值:{{sum}}</h1>
</template>
<style scoped>
</style>